package com.erp.module.packPiece.packPieceGather

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import android.widget.AdapterView
import com.erp.activity.BaseActivity
import com.erp.myAdapter.ArrayAdapter
import com.erp.myAdapter.PackPieceAddAdapter
import com.erp.bean.PackPieceBean
import com.erp.CaptureActivity
import com.erp.permission.PermissionListener
import com.erp.R
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import com.erp.view.MyAlerDialog
import com.erp.module.packPiece.PackPieceContract
import com.erp.module.packPiece.PackPieceP
import kotlinx.android.synthetic.main.activity_packpiece.*
import kotlinx.android.synthetic.main.header_knoya.*

/**
 * Created by Administrator on 2018/2/5/005.
 * 作者：刘洋
 * 拼装包装计件采集采集界面
 */

class PackPieceGatherAvtivity : BaseActivity() , PackPieceContract.PackPiecePutView, View.OnClickListener, AdapterView.OnItemLongClickListener {


    lateinit var gonghao: MutableList<String>
    lateinit var gonghaoname: MutableList<String>



    lateinit var KP : PackPieceContract.PackPieceKP
    lateinit var mlist : MutableList<PackPieceBean>
    lateinit var updialog: AlertDialog
    lateinit var adapter : PackPieceAddAdapter
    override fun getLayoutId(): Int {
        return R.layout.activity_packpiece
    }

    override fun initCreate() {
        gonghaoname = java.util.ArrayList()
        gonghao = java.util.ArrayList()
        mlist=ArrayList()
        KP= PackPieceP(mlist, this, this)
        adapter= PackPieceAddAdapter(mlist, this)
        LvPackPiece.adapter=adapter
        LvPackPiece.onItemLongClickListener = this
        iv_header_left.visibility=View.VISIBLE
        iv_header_left.setOnClickListener(this)
        BtLeft.setOnClickListener(this)
        BtRight.setOnClickListener(this)
        AtGongHao.setOnClickListener { AtGongHao.showDropDown() }
        tv_header_title.text="拼装包装采集"
        KP.getGonghao()
    }

    override fun putPackPieceOk() {
        val resultIntent = Intent()
        setResult(Activity.RESULT_OK, resultIntent)
        finish()
    }

    override fun putPackPieceNo() {
        updialog.dismiss()
    }

    override fun onClick(v: View?) {
        when(v){
            BtLeft-> PermissionUtils.issaomiao(this, object : PermissionListener {
                override fun onSucceed(requestCode: Int, grantPermissions: List<String>) {
                    val intent = Intent()
                    intent.setClass(this@PackPieceGatherAvtivity, CaptureActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                    startActivityForResult(intent, 101)
                }

                override fun onFailed(requestCode: Int, deniedPermissions: List<String>) {
                    Myutil.MyTost("没有权限，无法扫描")
                }
            })
            BtRight->{
                var work_staff_id =""
                for (i in gonghaoname.indices) {
                    if (gonghaoname[i] == AtGongHao.text.toString().trim { it <= ' ' }) {
                        work_staff_id= gonghao[i]
                        continue
                    }
                }
                if(work_staff_id==""){
                    Myutil.MyTost("必须要输入员工")
                    return
                }
                updialog = Myutil.showupdatedialog(this, "正在上传", false)
                KP.putPackPiece(work_staff_id)
            }
            iv_header_left -> {
                finish()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null||resultCode!= Activity.RESULT_OK){
            return
        }
        when(requestCode){
            101 -> {
                val bundle = data.extras
                val s = (bundle?.get("result") as String).replace("\"","")
                val datas = s.split("&")
                if(datas.size<5){
                    Myutil.MyTost("二维码不正确")
                    return
                }
                val bean = PackPieceBean()
                bean.cid=datas[0]
                bean.batch_name=datas[1]
                bean.gangwei_id=datas[2]
                bean.count=datas[3]
                bean.cnum=datas[4]
                for(i in 0 until mlist.size){
                    var b = mlist[i]
                    if((bean.batch_name == b.batch_name)&&(bean.cid==b.cid)&&(bean.gangwei_id==b.gangwei_id)){
                        Myutil.MyTost("不能重复扫描")
                        return
                    }
                }
                mlist.add(bean)
                adapter.notifyDataSetChanged()
            }
        }
    }

    override fun getGonghaoOK(gonghaoid: MutableList<String>, gonghaoname: MutableList<String>) {
        this.gonghao = gonghaoid
        this.gonghaoname = gonghaoname
        val av = ArrayAdapter(this@PackPieceGatherAvtivity, android.R.layout.simple_dropdown_item_1line, gonghaoname)
        AtGongHao.setAdapter(av)
    }

    override fun getGonghaoNO() {
    }

    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        when(parent){
            LvPackPiece -> {
                val myAlerDialog = MyAlerDialog(this@PackPieceGatherAvtivity)
                myAlerDialog.start()
                myAlerDialog.setdata("您确定要删除这一条吗？")
                myAlerDialog.setleft("确定") {
                    mlist.removeAt(position)
                    adapter.notifyDataSetChanged()
                    myAlerDialog.dismiss()
                }
                myAlerDialog.setright("取消") {myAlerDialog.dismiss()}
                myAlerDialog.show()
                return true
            }
        }
        return false
    }
}
